// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var chkName = 'checkBox'; // this is being set via script
var chkNameTrp = 'checkBoxTrp';
var chkNameEmp = 'checkBoxEmp';
var amountName = 'amount'; // this is being set via script
var detailName = 'details'; // this is being set via script
var comboName = 'combo'; // this is being set via script
var checkName = 'expenseDetailsCheck'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'ExpenseDetails'; // this should be named in the HTML
var TRIP_TABLE_NAME = 'TripDetails';
var TRIP_JOURNEY_TABLE_NAME ='TripDetailsJourney';// 'SearchTripJourney';//'TripSearchforJourney';
var EMP_TABLE_NAME ='EmpDetails';
var ROW_BASE = 1; // first number (for display)
var hasLoaded = true;

function fillInRows()
{
	hasLoaded = true;
	addRowToTable();
	alert("I am in fill in rows  ");
}

// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four)
{
	this.one = one; // input checkbox object
	this.two = two; // input select box
	this.three = three; // input text object
	this.four = four; // input text object
}


/*
 * insertRowToTable
 * Insert and reorder
 */
function insertTripDetails(routeId,tripId,serviceName,tripTime,from,to)
{
	if (hasLoaded) {
		var tbl = document.getElementById(TRIP_TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		}
		addTripDetails(routeId,tripId,serviceName,tripTime,from,to,rowToInsertAt);
	}
}


/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addTripDetails(routeId,tripId,serviceName,tripTime,from,to,num)
{//alert("@@Trip Time:"+tripTime);
	if (hasLoaded) {
		//alert("I am in add Trip Details if condition");
		var tbl = document.getElementById(TRIP_TABLE_NAME);
		//alert("I am in add Trip Details tbl" + tbl.id);
		var nextRow = tbl.tBodies[0].rows.length;
		//alert("I am in add Trip Details next row" + nextRow);
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		//alert("I am in add Trip Details num" + num);
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		//alert("I am in add Trip Details row" + row);		
		// CONFIG: requires classes named classy0 and classy1
		row.className = 'bodytext';
		//alert("I am in add Trip Details num" + row.className);

		// cell 1 - input checkbox
		var cell0 = row.insertCell(0);
		var cbEl = document.createElement('input');
		cbEl.setAttribute('type', 'checkbox');
		cbEl.setAttribute('name', chkName + iteration);
		cbEl.setAttribute('value', routeId + "/" + tripId);
		cell0.appendChild(cbEl);
		//alert("I am in add Trip Details cell1" + cell1);

		// CONFIG: This whole section can be configured
		// cell 0 - text
		var cell1 = row.insertCell(1);
		var routeIdText = document.createTextNode(routeId);
		cell1.appendChild(routeIdText);
		//alert("I am in add Trip Details cell0" + cell0);


		// cell 2 - text
		var cell2 = row.insertCell(2);
		var tripIdText = document.createTextNode(tripId);
		cell2.appendChild(tripIdText);
		//alert("I am in add Trip Details cell2" + cell2);
		
		// cell 3 - text routeId,tripId,serviceName,tripTime,from,to
		var cell3 = row.insertCell(3);
		var serviceNameText = document.createTextNode(serviceName);
		cell3.appendChild(serviceNameText);
		//alert("I am in add Trip Details cell3" + cell3);
		
		// cell 4 - text
		var cell4 = row.insertCell(4);
		var tripTimeText = document.createTextNode(tripTime);
		cell4.appendChild(tripTimeText);
		//alert("I am in add Trip Details cell4" + cell4);

		// cell 5 - text
		var cell5 = row.insertCell(5);
		var fromText = document.createTextNode(from);
		cell5.appendChild(fromText);
		//alert("I am in add Trip Details cell5" + cell5);		

		// cell 6 - text
		var cell6 = row.insertCell(6);
		var toText = document.createTextNode(to);
		cell6.appendChild(toText);
		//alert("I am in here cell 6");
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectTrip(cbEl , routeIdText , tripIdText , serviceNameText , tripTimeText , fromText , toText);
	}
}


//---------------------------Journey   ---------------------------------------------
/*
 * insertRowToTable
 * Insert and reorder
 */
function insertTripDetailsJourney(tripId,tripTime)
{
	//alert("@@Insert-TripId:"+tripId+"TripTime:"+tripTime);
	if (hasLoaded) {
		//alert("Hi");
		//alert("TableName:"+TRIP_JOURNEY_TABLE_NAME);
		var tbl = document.getElementById(TRIP_JOURNEY_TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		//alert(rowToInsertAt);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		
		}
		//alert("@@addTrip-call:");
		addTripDetailsJourney(tripId,tripTime,rowToInsertAt);
	}
}


/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addTripDetailsJourney(tripId,tripTime,num)
{
	//alert("@@addTrip-TripId:"+tripId+"TripTime:"+tripTime);
	if (hasLoaded) {
		//alert("a.I am in add Trip Details if condition-1");
		var tbl = document.getElementById(TRIP_JOURNEY_TABLE_NAME);
		//alert("b.I am in add Trip Details tblId:" + tbl.id);
		var nextRow = tbl.tBodies[0].rows.length;
		//alert("c.I am in add Trip Details next row:" + nextRow);
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
			//alert("d-0.IFnorow:" + num);
		} else {
			iteration = num + ROW_BASE;
			//alert("d-1.ELSEnorow:" + iteration);
		}
		//alert("2.I am in add Trip Details num:" + num);
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		//alert("3.I am in add Trip Details row:" + row);		
		// CONFIG: requires classes named classy0 and classy1
		row.className = 'bodytext';
		//alert("33.I am in add Trip Details num:" + row.className);

		// cell 1 - input checkbox
		var cell0 = row.insertCell(0);
		var cbEl = document.createElement('input');
		cbEl.setAttribute('type', 'checkbox');
		cbEl.setAttribute('name', chkNameTrp + iteration);
		//alert("44.I am in add Create row num:" + row.className);
		cbEl.setAttribute('value', tripId + "/" + tripTime);
		cell0.appendChild(cbEl);
		//alert("4.I am in add Trip Details cell1" + cell1);

		// CONFIG: This whole section can be configured
		// cell 0 - text
		var cell1 = row.insertCell(1);
		var tripIdText = document.createTextNode(tripId);
		cell1.appendChild(tripIdText);
		//alert("5.I am in add Trip Details cell0" + cell0);

		// cell 2 - text
		var cell2 = row.insertCell(2);
		var tripTimeText = document.createTextNode(tripTime);
		cell2.appendChild(tripTimeText);
		//alert("6.I am in add Trip Details cell2" + cell2);
		
		
		
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectTrip(cbEl , tripIdText , tripTimeText );
	}
}
//------------------------------------Ene Journey---------------------------------------------------------------------------

//---------------------------Employee---------------------------------------------------------------------------------
/*
 * insertRowToTable
 * Insert and reorder
 */
function insertEmployeeDetailsJourney(empId, empName, empType)
{
	//alert("@@Insert-EmpId:"+empId+"EmpTime:"+empName+"EmpType:"+empType);
	if (hasLoaded) {
		//alert("Hi");
		//alert("TableName:"+EMP_TABLE_NAME);
		var tbl = document.getElementById(EMP_TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		}
		//alert("@@addEmployee-call:");
		addEmployeeDetailsJourney(empId, empName, empType,rowToInsertAt);
	}
}


/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addEmployeeDetailsJourney(empId, empName, empType,num)
{
	//alert("@@addTrip-EmpId:"+empId+"EmpTime:"+empName+"EmpType:"+empType);
	if (hasLoaded) {
		//alert("a.I am in add Trip Details if condition-1");
		var tbl = document.getElementById(EMP_TABLE_NAME);
		//alert("b.I am in add Trip Details tblId:" + tbl.id);
		var nextRow = tbl.tBodies[0].rows.length;
		//alert("c.I am in add Trip Details next row:" + nextRow);
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
			//alert("d-0.IFnorow:" + num);
		} else {
			iteration = num + ROW_BASE;
			//alert("d-1.ELSEnorow:" + iteration);
		}
		//alert("2.I am in add Trip Details num:" + num);
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		//alert("3.I am in add Trip Details row:" + row);		
		// CONFIG: requires classes named classy0 and classy1
		row.className = 'bodytext';
		//alert("33.I am in add Trip Details num:" + row.className);

		// cell 1 - input checkbox
		var cell0 = row.insertCell(0);
		var cbEl = document.createElement('input');
		cbEl.setAttribute('type', 'checkbox');
		cbEl.setAttribute('name', chkNameEmp + iteration);
		//alert("44.I am in add Create row num:" + row.className);
		cbEl.setAttribute('value', empId + "/" + empName);
		cell0.appendChild(cbEl);
		//alert("4.I am in add Trip Details cell1" + cell0);

		// CONFIG: This whole section can be configured
		// cell 0 - text
		var cell1 = row.insertCell(1);
		var empIdText = document.createTextNode(empId);
		cell1.appendChild(empIdText);
		//alert("5.I am in add Trip Details cell0" + cell1);

		// cell 2 - text
		var cell2 = row.insertCell(2);
		var empNameText = document.createTextNode(empName);
		cell2.appendChild(empNameText);
		//alert("6.I am in add Trip Details cell2" + cell2);
		
		var cell3 = row.insertCell(3);
		
		
		//alert("EmpType-----------------------::"+empTypeCont);
		var empTypeText = document.createTextNode(empType);
		//alert("EmpTypeText::"+empTypeText);
		cell3.appendChild(empTypeText);
		//alert("6.I am in add Trip Details cell2" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		
		
		
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObjectTrip(cbEl , empIdText , empNameText , empTypeText );
	}
}

function deleteEmployRow()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		
		var tbl = document.getElementById(EMP_TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.one.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.one.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
				
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

//--------------------------------END--Employee----------------------------------------------------------------
function deleteTripRowJourney()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		
		var tbl = document.getElementById(TRIP_JOURNEY_TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.one.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.one.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
				
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

//------------------------------------------------------------------------

function myRowObjectTrip(one)
{
	this.one = one; // input checkbox object
}

/*
 * insertRowToTable
 * Insert and reorder
 */
function insertRowToTable()
{
	if (hasLoaded) {
		var tbl = document.getElementById(TABLE_NAME);
		var rowToInsertAt = tbl.tBodies[0].rows.length;
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {			
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.four.getAttribute('type') == 'radio' && tbl.tBodies[0].rows[i].myRow.four.checked) {
				rowToInsertAt = i;
				break;
			}
		}
		addRowToTable(rowToInsertAt);
		//reorderRows(tbl, rowToInsertAt);
	}
}




/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */
function addRowToTable(num)
{
	if (hasLoaded) {
		//alert("I am in add Row To Table if condition");
		var tbl = document.getElementById(TABLE_NAME);
		//alert("I am in add Row To Table id is " + tbl);
		var nextRow = tbl.tBodies[0].rows.length;
		//alert("I am in add Row To Table nextRow " + nextRow);
		var iteration = nextRow + ROW_BASE;
		//alert("I am in add Row To Table iteration " + iteration);
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		//alert("I am in add Row To Table num " + num);		
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		row.setAttribute('id', iteration);
		//alert("I am in add Row To Table iteration row " + row);		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'inputform100';
	
		// CONFIG: This whole section can be configured
		
		// cell 1 - input checkbox
		var cell0 = row.insertCell(0);
		var cbEl = document.createElement('input');
		cbEl.setAttribute('type', 'checkbox');
		cbEl.setAttribute('name', checkName + iteration);
		cell0.appendChild(cbEl);

		//alert("I am in add Row To Table iteration cell1 " + cell0);

		// cell 2 - drop down select
		var cell1 = row.insertCell(1);
		var cbe2 = document.createElement('select');
		cbe2.setAttribute('name', comboName + iteration);
		//alert(cbe2.name + " / " + comboName + iteration);
		//cbe2.setAttribute('size','1');
		//cbe2.setAttribute('option', 'select');
		//btnEl.onclick = function () {deleteCurrentRow(this)};
		cell1.appendChild(cbe2);
		//alert("I am in add Row To Table iteration cell2 " + cell1);
		
		// cell 3 - input text
		var cell2 = row.insertCell(2);
		var txtInp = document.createElement('input');
		txtInp.setAttribute('type', 'text');
		txtInp.setAttribute('name', amountName + iteration);
		txtInp.className = 'inputform100';
//		txtInp.setAttribute('size', '30');
		txtInp.setAttribute('value', 0); // iteration included for debug purposes
		cell2.appendChild(txtInp);
		//alert("I am in add Row To Table iteration cell3 " + cell2);		
		
		// cell 4 - input text
		var cell3 = row.insertCell(3);
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', detailName + iteration);
//		txtInp1.setAttribute('size', '40');
//		txtInp1.setAttribute('value', iteration); // iteration included for debug purposes
		txtInp1.className = 'inputform100';
		cell3.appendChild(txtInp1);

		//alert("I am in add Row To Table iteration cell4 " + cell3);		
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(cbEl, cbe2, txtInp, txtInp1);
		moveSelectedTypes(cbe2);
	}
}

// CONFIG: this entire function is affected by myRowObject settings
// If there isn't a checkbox in your row, then this function can't be used.
function deleteChecked()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		
		var tbl = document.getElementById(TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.two.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.two.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
				
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

function deleteCheckedEdit()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		var tbl = document.getElementById(TABLE_NAME);
		for (var i = 0; i < document.BusExpense.elements.length; i++) {
			var e = document.BusExpense.elements[i];
			if (e.type == 'checkbox' && e.checked == true) {
				if( e.name.substring(0,19) == 'expenseDetailsCheck') {
					var row=e.parentNode;
					while (row.nodeName!="TR") {
						row=row.parentNode;
					}
					tbl.deleteRow(row.rowIndex);
				}
			}
		}
	}
}

function deleteTripRow()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		
		var tbl = document.getElementById(TRIP_TABLE_NAME);
		for (var i = 0; i < document.BusExpense.elements.length; i++) {
			var e = document.BusExpense.elements[i];
			if (e.type == 'checkbox' && e.checked == true) {
				if( e.name.substring(0,8) == 'checkBox') {
					var row=e.parentNode;
					while (row.nodeName!="TR") {
						row=row.parentNode;
					}
					tbl.deleteRow(row.rowIndex);
				}
			}
		}
	}
}

function deleteTripRow1()
{
	if (hasLoaded) {
		var checkedObjArray = new Array();
		var cCount = 0;
		
		var tbl = document.getElementById(TRIP_TABLE_NAME);
		for (var i=0; i<tbl.tBodies[0].rows.length; i++) {
		
			if (tbl.tBodies[0].rows[i].myRow && tbl.tBodies[0].rows[i].myRow.one.getAttribute('type') == 'checkbox' && tbl.tBodies[0].rows[i].myRow.one.checked) {
				checkedObjArray[cCount] = tbl.tBodies[0].rows[i];
				cCount++;
				
			}
		}
		if (checkedObjArray.length > 0) {
			var rIndex = checkedObjArray[0].sectionRowIndex;
			deleteRows(checkedObjArray);
			reorderRows(tbl, rIndex);
		}
	}
}

// If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		//reorderRows(tbl, rIndex);
	}
}

function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
			
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.two.name = comboName + count; // input text
				
				// CONFIG: next line is affected by myRowObject settings
				var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
				tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}